home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer Conversion
- Date: 21 Jan 1996 17:51:30 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <TANMOY.96Jan21105130@qcd.lanl.gov>
- References: <4ds4jq$fo4@su3.in.net> <20JAN199622465412@erich.triumf.ca>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: bennett@erich.triumf.ca's message of 20 Jan 1996 22:46 PST
-
- --text follows this line--
- In article <20JAN199622465412@erich.triumf.ca> bennett@erich.triumf.ca
- (P.Bennett) writes:
- <snip>
- >char *my_strcat(const char *a, const char *b)
- >{
- > char done[1024];
- <snip>
- > return done; /* this is the suspicious pointer conversion error */
- >}
-
- Your function is declared to return a char pointer, and you are returning an
- array, which is not the same thing. Sometimes char array names act like char
- pointers (particularly when used as function parameters), but not here.
-
-
- You are correct in stating that an array is not a pointer, but in this
- context, the _value_ of an array is being returned. Except as an
- immediate operand of sizeof or &, an array always decays to its value
- which is a pointer to its first element. So, at this point, the return
- value is of the correct type.
-
-
- A more important problem here is that "done" is an automatic array which will
- cease to exist when the function returns, possibly being over-written on the
- next call to another function.
-
- This is the actual problem.
-
- You could declare "done" as a char pointer, and malloc() some memory for it to
- point to - this would solve both problems, but the calling function would have
- to free() that malloc()ed memory sometime.
-
- Correct.
-
- You could also declare done as a static array (just add "static" before the
- present declaration), then the array will exist for the life of the program.
- I _think_ (and I know someone will correct me if I'm wrong) that changeing
- "return done;" to "return &done;" will fix the "suspicious pointer conversion"
- error.
-
- Wrong. You can use static arrays, but &done has the type pointer to
- character array of length 1024, i.e. char(*)[1024]. This is not the
- same as char *.
-
- done itself decays to char *.
-
- (Never take the warnings from an unknown compiler too literally).
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-